<?php
/**
 * 前台设计师专用
 * @author cjli
 *
 */ 
class DesignerAction extends HomeAction
{
	public function _initialize()
	{
		parent::_initialize();
		
		send_http_status(404);
		$this->display(C('ERROR_PAGE'));
		exit;  
//		global $user;
//		if(! UserModel::logged()){
//			redirect(url('user/login'),'', L('_USER_NO_LOGINED_'));
//		}
//		if(! $user->is_designer){
//			redirect(url('user/center'),'', L('_USER_NO_DESIGNER_'));
//		}
	} 
	
	public function index()
	{
		$where = array(
			'is_designer' => 1,
			'user_designer.user_id' => array('gt', 0),
		);
		$user_db = D('users');
		$user_designer_db = D('user_designer');
		
		$pageRows = 6;
		
		$userCount = $user_db->where($where)->count();
		require THEMES_PATH.'page.php';
		$Page = new Page($userCount, $pageRows);
		$pageShow = $Page->show();
		$data['pageShow'] = $pageShow;
		
		$user_db->field('users.nickname,user_id,user_designer_logo,user_designer_adv,user_content,user_description');
		$user_db->join('user_designer ON users.id = user_designer.user_id');
		$user_db->where($where);
		//$user_db->limit($Page->firstRow, $Page->listRows);
		$designerList = $user_db->select();
		$photos = array();
		if($designerList){
			$i =0;
			foreach ($designerList as &$users){
				
				//$users['productList'] = ProductModel::get_product_list(array('designer_id' => $users['user_id'], 'orderby' => 'product.is_sell DESC, product.id DESC'), 1, 4);														   
				//$productList = ProductModel::get_product_list(array('designer_id' => $users['user_id'], 'orderby' => 'product.is_sell DESC, product.id DESC'), 1, 999999999);	
				$designer_product_db = D('designer_product');
				$productList = $designer_product_db->where('user_id='.$users['user_id'])->order('id DESC')->select();
				if($i > 0)
					$i = $i+1;
				$users['user_content'] = strip_tags($users['user_content']);
				$photos[$i]['nickname'] 			= $users['nickname'];
				$photos[$i]['user_id'] 				= $users['user_id'];
				$photos[$i]['user_designer_logo'] 	= $users['user_designer_logo'];
				$photos[$i]['user_designer_adv'] 	= $users['user_designer_adv'];
				$photos[$i]['user_content'] 		= $users['user_content'];
				$photos[$i]['user_description'] 	= $users['user_description'];

				foreach($productList as $product){
					$photos[$i+1]['id'] 			= $product['id'];
					$photos[$i+1]['list_img'] 		= $product['list_img'];
					$i++;
					
				}
			}
		}
//dump($designerList);exit;
		//for($j=0;$j<=count($photos);$j++){
//			print_r($photos[$j]);
//			echo "<br>";
//		}
		
		$this->assign('photos', $photos);
		$this->display('designer_index');
	}
	
	
	//设计师信息
	public function user()
	{
		$designer_id = intval($_REQUEST['did']);
		$userInfo = UserModel::get_user_info($designer_id);
		if(! $userInfo || $userInfo['is_designer']==0 || $userInfo['disabled']==0){
			redirect(url('designer/index'), L('_ERROR_ACTION_'));
		}
		
		$where = array(
			'user_id' => $designer_id,
			'display' => 1,
			'product_id' => 0,
		);
		
		$designerInfo = UserModel::get_user_designer_info($designer_id);
		$designerInfo = array_merge($userInfo, $designerInfo);
		
		//$productCount = ProductModel::get_product_count($where);
		$designer_product_db = D('designer_product');
		$productCount = $designer_product_db->where($where)->count();
		require THEMES_PATH.'page.php';
		$Page = new Page($productCount, $pageRows);
		$pageShow = $Page->show();
		$data['pageShow'] = $pageShow;
		
		$designer_product_db->where($where);
		$designer_product_db->limit($Page->firstRow, $Page->listRows);
		$productList = $designer_product_db->order('id DESC')->select();
		
		//$productList = ProductModel::get_product_list($where, $Page->firstRow, $Page->listRows);
		
		$where2 = array(
			'user_id' => $designer_id,
			'is_sell' => 1,
			'orderby' => 'product.is_sell DESC, product.id DESC',
		);
		$productSellList = ProductModel::get_product_list($where2, 1, 4);
				
//		dump($designerList);exit;
		$this->assign('designerInfo', $designerInfo);
		$this->assign('productList', $productList);
		$this->assign('productSellList', $productSellList);
		$this->assign($data);
		$this->display('designer_user');
	}
	
	//设计师作品信息
	public function info()
	{
		$pid = intval($_GET['pid']);
		
		$designer_product_db = D('designer_product');
		if($pid){
			$where = array(
				'language_id' => C('language_id'),
				'id' => $pid,
			);
			$productInfo = $designer_product_db->where($where)->find();
		}
			
		$product = ProductModel::get_product_info($productInfo['product_id']);
		if(!$productInfo || $product['is_sell']==1){
			redirect(url('designer/index'), L('_ERROR_ACTION_'));
		}
		
		$user_id = $productInfo['user_id'];
		$userInfo = UserModel::get_user_info($user_id);
		if(! $userInfo || $userInfo['is_designer']==0 || $userInfo['disabled']==0){
			redirect(url('designer/index'), L('_ERROR_ACTION_'));
		}
		
		$designerInfo = UserModel::get_user_designer_info($user_id);
		$designerInfo = array_merge($userInfo, $designerInfo);
		
		$where = array(
			'user_id' => $user_id,
			'display' => 1,
		);
		
		//$productCount = ProductModel::get_product_count($where);
//
//		if($productCount>=5){
//			$where['orderby'] = 'rand()';
//			$productSellList = ProductModel::get_product_list($where, 1, 5);
//		}else{
//			$where['is_sell'] = null;
//			$productSellList = ProductModel::get_product_list($where, 1, 5);
//		}
		$where['id']  = array('neq',$pid);
		$designer_product_db->where($where);
		$designer_product_db->limit(1, 5);
		$productSellList = $designer_product_db->order('id DESC')->select();
	
		$productInfo['photoList'] = ProductModel::get_product_photo_list($productInfo['product_id'],$pid);
		$properties = ProductModel::get_product_relation_attribte_list($productInfo['product_id'], array('attr_designer' => 1),$pid);// 获得商品的规格和属性
		$this->assign('properties', isset($properties['pro']) ? $properties['pro'] : null);                              // 商品属性
		$this->assign('specification', isset($properties['spe']) ? $properties['spe'] : null);
			
		//评论
		$commentlist = CommentModel::get_product_comment_list($productInfo['product_id'],$pid);
		$productUpDown = ProductModel::get_product_up_down($productInfo['product_id'],$pid);
		
		$this->assign('designerInfo', $designerInfo);
		$this->assign('productInfo', $productInfo);
		$this->assign('productSellList', $productSellList);
		$this->assign('commentlist', $commentlist);
		$this->assign('productUpDown', $productUpDown);
		$this->display('designer_info');
	}
	
	
	public function comment()
	{
	
	}
	
	/**
	 * ajax提交评论
	 */
	public function comment_add()
	{
		global $user;
		$result = array(
			'error' => 1,
			'message' => '',
		);
		$pid = intval($_REQUEST['pid']);
		$designer_product_db = D('designer_product');
		$productInfo = $designer_product_db->find($pid);
		//$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$result['message'] = L('_PRODUCT_NO_EXIST_');
			echo json_encode($result);exit;
		}

		$post = $_REQUEST;
		if($post)
		{
			if($_SESSION['designer_vcode'] != strtolower($post['designer_vcode'])){
				$result['message'] = L('_VERIFICATION_ERROR_');
				echo json_encode($result);exit;
			}
			
			if(empty($post['username']) || empty($post['content'])){
				$result['message'] = L('_PLEASE_INPUT_CORRECT_OCCUPATIONAL_INFORMATION_');
				echo json_encode($result);exit;
			}
			
			$post['dproduct_id'] = $pid;
			$post['designer_id'] = $productInfo['user_id'];
			if($comment_id = CommentModel::insert_comment($post)){
				$result['message'] = L('_OPERATION_SUCCESS_');
				$result['error'] = 0;
				$result['comment_id'] = $comment_id;
				echo json_encode($result);exit;
			}else{
				$result['message'] = L('_OPERATION_FAIL_');
				echo json_encode($result);exit;
			}
		}else{
			$result['message'] = L('_ERROR_ACTION_');
				echo json_encode($result);exit;
		}
		
	}
	
	//评论回复
	public function comment_replay()
	{
		global $user;
		$result = array(
			'error' => 1,
			'message' => '',
		);
		$parent_id = intval($_REQUEST['comment_id']);
		$content = trim($_REQUEST['content']);
		if($parent_id==0){
			$result['message'] = L('_SELECT_REPLAY_COMMENT_');
		}
		elseif(empty($content)){
			$result['message'] = L('_REPLAY_COMMENT_NO_EMPTY_');
		}
		else{
			$com_db = D('comment');
			$data = array(
					'parent_id' => $parent_id,
					'username' => $user['nickname'] ? $user['nickname'] : ($user['first_name']. ' '. $user['last_name']),
					'content' => $content,
					'comment_time' => time(),
				);
				$sub_id = $com_db->add($data);
			if($sub_id){
				$data['time'] = date('Y-m-d H:i:s', $data['comment_time']);
				$result = array(
					'error' => 0,
					'message' => L('_OPERATION_SUCCESS_'),
					'info' => $data,
				); 
			}else{
				$result['message'] = L('_OPERATION_FAIL_');
			}
		}
		echo json_encode($result);exit;
	}
	
	//获取顶后的是否预售ajax
	public function get_reserve()
	{
		$result = array(
			'error' => 1,
			'message' => '',
		);
		$pid = intval($_REQUEST['pid']);
		$productInfo = ProductModel::get_product_info($pid);
		if(!$productInfo){
			$result['message'] = L('_PRODUCT_NO_EXIST_');
		}else{
			$productInfo['presellCount'] = OrderModel::get_presell_product_people_count($pid);
			$this->assign('productInfo', $productInfo);
			$message = $this->fetch('public/block_reserve');
			$result = array(
			'error' => 0,
			'message' => $message,
		);
		}
		
		echo json_encode($result);exit;
	}

	//评论加赞ajax
	public function ping_zan()
	{
		$result = array(
			'error' => 1,
			'message' => '',
		);
		
		$pid = intval($_REQUEST['pid']);
		$up_down = trim($_REQUEST['up_down']);
		//$productInfo = ProductModel::get_product_info($pid);
		$designer_product_db = D('designer_product');
		$productInfo = $designer_product_db->find($pid);
		if(!$productInfo){
			$result['message'] = L('_PRODUCT_NO_EXIST_');
		}else{
			$productUpDown = ProductModel::get_product_up_down($productInfo['product_id'],$pid);
			$product = array(
				'id' => $productInfo['id'],
				'up_down' => $up_down ? $up_down : null,
				'product_up' => isset($productUpDown['product_up']) ? $productUpDown['product_up'] : 0,
				'product_down' => isset($productUpDown['product_down']) ? $productUpDown['product_down'] : 0,
			);
			$this->assign('productInfo', $product);
			$message = $this->fetch('public/block_pingzan');
			$result = array(
			'error' => 0,
			'message' => $message,
		);
		}
		echo json_encode($result);exit;
	}

/******************************************会员中心***********************************/
	
//会员中心-设计师中心
	public function center()
	{
		global $user;
		
		$this->check_designer();
		
		$this->work_list();
	}
	
	//会中中心-作品中心
	public function work_list()
	{
		global $user;
		
		$this->check_designer();
		
		$user_id = $user['id'];
		
		$where = array(
			'user_id' => $user_id,
		);
		
		$designerInfo = UserModel::get_user_designer_info($user_id);
		$designerInfo = array_merge($user, $designerInfo);
		
		$designer_product_db = D('designer_product');
		$productList = $designer_product_db->where($where)->order('id DESC')->select();
		//$productList = ProductModel::get_product_list($where);
		$productWorkList = $productSellList = array();
		if($productList){
			foreach ($productList as $product){
				if($product['is_sell']==1){
					$product['orderList'] = OrderModel::get_product_order_sharing($product['product_id']);
				}else{
					$product['commentList'] = CommentModel::get_product_comment_list($pid=0,$product['id']);
					$productUpDown = ProductModel::get_product_up_down($pid=0,$product['id']);
					$product['product_up'] = $productUpDown['product_up'];
					$product['product_down'] = $productUpDown['product_down'];
				}
				
				$product['commentCount'] = CommentModel::get_product_comment_count($product['product_id'],$product['id']);
				
				$product['presellCount'] = OrderModel::get_presell_product_people_count($product['product_id']);
				
				if($product['product_id'])
					$productInfo = ProductModel::get_product_info($product['product_id']);
				
				if($productInfo['is_sell']==1 && $product['product_id']>0){
					$productSellList[] = $productInfo;
				}else{
					$productWorkList[] = $product;
				}
			}
		}
		
		$this->assign('designerInfo', $designerInfo);
		$this->assign('productWorkList', $productWorkList);
		$this->assign('productSellList', $productSellList);
		$this->display('work_list');
	}
	
	//会中中心-上传作品
	public function work_edit()
	{
		global $user;
		
		$this->check_designer();
		
		$pid = intval($_REQUEST['id']);
		
		$designer_product_db = D('designer_product');
		if($pid){
			$where = array(
				'language_id' => C('language_id'),
				'id' => $pid,
			);
			$productInfo = $designer_product_db->where($where)->find();
		}
		if($productInfo['display'] > 0){
			$this->error(L('the_work_has_been_approved'), url('designer/work_list'));
		}
		
		if(isset($_POST['dosubmit'])){
//			dump($_POST);exit;
			$post = $_POST;
			
			//上传图片到产品表
			if(isset($post['focus_photo']) && $post['focus_photo']){
				$img = $post['focus_photo']; //原图
				$dir = dirname($img);
				$img_name = basename($img);
				$post['img'] = $img;
				$post['small_img'] = $dir . '/thumb_' . $img_name; //小图片
				$post['list_img'] = $dir . '/list_' . $img_name; //列表图片
			}	
			$post['model_id'] = 1;
			$post['catid'] = 1950;
			
			$data = array(
				'language_id' => C('language_id'),
				'name' => trim($post['name']),
				'content' => trim($post['content']),
				'production_type' => $post['production_type'],
				'ip' => get_client_ip(),
			);
			if($post['img']){
				$data['img'] = $post['img'];
				$data['small_img'] = $post['small_img'];
				$data['list_img'] = $post['list_img'];
			}
			$designer_product_db = D('designer_product');
			$designerInfo = $designer_product_db->where('id='.$pid)->find();
			if($designerInfo){
				$designer_product_db->where(array('id' => $pid))->save($data);
			}else{
				$data['time'] = time();
				$data['user_id'] = $user['id'];
				$data['username'] = $user['first_name'];
				$data['product_id'] = 0;
				$data['display'] = 0;
				$data['designer_up'] = 0;
				$data['designer_down'] = 0;
				$data['model_id'] = 1;
				$data['catid'] = 1950;
				$pid = $designer_product_db->add($data);
			}
			
			if($productInfo['product_id'] =='' or $productInfo['product_id']=='null')
				$productInfo['product_id'] = 0;	
			//图片关联入库
			$this->insert_photo($productInfo['product_id'], $post,$pid);
			//属性入库
			$this->insert_attribute_relation(0, $post,$pid);
			
			$this->success(L('_WORK_EDIT_SUCCESS_'), U('designer/work_list'));
		}
		else{
			$product_photo_ids = '';
			if($productInfo){
				$photoList = ProductModel::get_product_photo_list($productInfo['product_id'],$pid);
				if($photoList){
					foreach ($photoList as $file){
						$product_photo_ids .= $file['fid'].',';
					}
				}
				
			}
			$attr_html = AttributeModel::get_attribute_values_html(1, $productInfo['product_id'], array('attr_designer' => 1),$pid);
		}
		
		//获取作品类型
		$production_type = CategoryModel::get_category_list($vid = 7);
		$this->assign('product_photo_ids', $product_photo_ids);
		$this->assign('productInfo', $productInfo);
		$this->assign('production_type', $production_type);
		$this->assign('attr_html', $attr_html);
		$this->assign('photoList', isset($photoList) ? $photoList : array());
		$this->display('work_edit');
	}
	
	public function myinfo()
	{
		global $user;
		
		$this->check_designer();
		
		$designerInfo = UserModel::get_user_designer_info($user['id']);
		$designerInfo = array_merge($user, $designerInfo);
		
		if(isset($_POST['dosubmit'])){
			$post = $_POST;
			if ( (isset($_FILES['up_designer_logo']) && $_FILES['up_designer_logo']['tmp_name']) || (isset($_FILES['up_designer_adv']) && $_FILES['up_designer_adv']['tmp_name'])){
				$info = $this->upload('user');
				if(is_array($info)) {// 上传成功 获取上传文件信息
					if(isset($info['up_designer_logo'])){
						$post['user_designer_logo'] = UPLOAD_PATH . $info['up_designer_logo']['savename'];
					}
					if(isset($info['up_designer_adv'])){
						$post['user_designer_adv'] = UPLOAD_PATH . $info['up_designer_adv']['savename'];
					}
				}else{
					$this->error($info);
				}
			}

			D('users')->where('id='.$user['id'])->save(array('nickname' => $post['nickname']));
			
			if( $this->insert_designer($user['id'], $post)){
				$this->success(L('_OPERATION_SUCCESS_'), url('designer/myinfo'));
			}else{
				$this->error(L('_OPERATION_FAIL_'), url('designer/myinfo'));
			}
		}

		$this->assign('designerInfo', $designerInfo);
		$this->display('designer_myinfo');
		
	}	
	
	//设计师验证
	private function check_designer()
	{
		global $user;
		if(! UserModel::logged()){
			redirect(url('user/login'),'', L('_USER_NO_LOGINED_'));
		}
		if(! $user['is_designer']){
			redirect(url('/help',array('id' => 4)), L('_ERROR_ACTION_'));
		}
	}
	
/**
	 * 产品图片关联入库
	 * @param int $pid 产品ID
	 * @param array $post post表单
	 */
	private function insert_photo($pid, $post,$did=0)
	{
		$product_photo_relation_db = D('product_photo_relation');
		$photo_ids = $post['photo_name'];
		if($photo_ids){
			$photo_ids = rtrim($photo_ids,',');
			$fids = explode(',', $photo_ids);
			if(count($fids)){
				if($did>0)
					$product_photo_relation_db->where('did='.$did)->delete();
				else
					$product_photo_relation_db->where('pid='.$pid)->delete();
				
				foreach ($fids as $fid){
					$data = array(
						'fid' => $fid,
						'pid' => $pid,
						'did' => $did,	
					);
					$product_photo_relation_db->add($data);
				}
			}
		}
	}
	
	/**
	 * 产品属性关联入库
	 * @param int $pid 产品ID
	 * @param array $post post表单
	 */
	private function insert_attribute_relation($pid, $post,$did=0)
	{
    	$product_attribute_db = D('product_attribute');
		$model_id = intval($post['model_id']);
		$attributeList = AttributeModel::get_attribute_list($model_id); // 属性列表
		if($pid > 0){
			//删除产品关联属性
			$product_attribute_db->where('product_id='.$pid)->delete();
		}else{
			//删除作品关联属性
			$product_attribute_db->where('designer_id='.$did)->delete();
		}

    	foreach ($attributeList as $attr){
			$post_field = $post['field_'.$attr['attr_id']]; //属性post字段
			//dump($post_field);exit;
			if(isset($post_field) && $post_field){
				 //下拉单选
				if(is_numeric($post_field))
				{
					$data = array(
						'product_id' => $pid,
						'designer_id' => $did,
						'attr_id' => $attr['attr_id'],
						'attr_value_id' => $post_field,
					);
					$product_attribute_db->add($data);
				}
				 //单选
				elseif(is_string($post_field))
				{
					$data = array(
						'product_id' => $pid,
						'designer_id' => $did,
						'attr_id' => $attr['attr_id'],
						'attr_value' => $post_field,
					);
					$product_attribute_db->add($data);
				}
				//复选
				elseif(is_array($post_field) && count($post_field)>0)
				{ 
					$is_default = 0;
					$attr_value_id = 0;
					$attr_value = '';
					//默认值
					if(isset($post_field['default'])){
						$is_default = $post_field['default'] ? intval($post_field['default']) : 0;
						unset($post_field['default']);
					}
					
					foreach ($post_field as $post_key => $post_value )
					{
						if($attr['attr_input_type']==1 && $post_value){
							$attr_value_id = intval($post_value);
							if($is_default==0 && $post_key==0){
								$is_default = $attr_value_id;
							}
						}
						elseif($attr['attr_input_type']==0 && $post_value){
							$attr_value = trim($post_value);
						}

						$data = array(
							'product_id' => $pid,
							'designer_id' => $did,
							'attr_id' => $attr['attr_id'],
							'attr_value_id' => $attr_value_id,
							'attr_value' => $attr_value,
							'is_default' => $attr_value_id==$is_default ? $is_default : 0,
						);
						$product_attribute_db->add($data);
						//echo $product_attribute_db->getLastSql();exit;
					}
				}
			}
		}
//		exit;
	}
	
	/**
	 * 添加设计师资料
	 * @param int $user_id 用户ID
	 * @param array $post post数组
	 */
	private function insert_designer($user_id, $post)
	{
		$designer_db = D('user_designer');
		$designer_db->where('user_id='.$user_id)->delete();

		$data = array(
			'user_id' => $user_id,
			'user_designer_logo' => $post['user_designer_logo'],
			'user_designer_adv' => $post['user_designer_adv'],
			'user_content' => trim($post['user_content']),
			'user_description' => trim($post['user_description'])
		);
		$lastId = $designer_db->add($data);
		return $lastId;
	}
	
}